package com.ocean.elementdict.service.impl;

import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.core.text.Convert;
import com.ocean.common.utils.DateUtils;
import com.ocean.common.utils.ShiroUtils;
import com.ocean.common.utils.uuid.IdUtils;
import com.ocean.elementdict.domain.OElementDict;
import com.ocean.elementdict.mapper.OElementDictMapper;
import com.ocean.elementdict.service.IOElementDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author msy
 * @date 2023/2/20
 */
@Service
public class OElementDictServiceImpl implements IOElementDictService {
    @Autowired
    private OElementDictMapper oElementDictMapper;

    /**
     * 查询列表
     * @param elementDict
     * @return
     */
    @Override
    public List<OElementDict> selectOElementDictList(OElementDict elementDict) {
        return oElementDictMapper.selectOElementDictList(elementDict);
    }

    /**
     * 获取所有英文名称
     * @return list  -> {"a","b"}
     */
    @Override
    public List<String> getElementEn() {
        List<Map<String,String>> enList = oElementDictMapper.getElementEn();
        List<String> result = enList.stream().map(m -> m.get("element_en")).collect(Collectors.toList());
        return result;
    }

    /**
     * 插入数据
     * @param elementDict
     * @return
     */
    @Override
    public int insertElementDict(OElementDict elementDict) {
        // 设置elementDict的ID为简单UUID
        elementDict.setId(IdUtils.simpleUUID());
        // 获取当前登录用户的SysUser
        SysUser sysUser = ShiroUtils.getSysUser();
        // 设置elementDict的创建者名称
        elementDict.setCreateName(sysUser.getUserName());
        // 设置elementDict的创建者账号
        elementDict.setCreateBy(sysUser.getLoginName());
        // 设置elementDict的创建时间
        elementDict.setCreateDate(DateUtils.getNowDate());
        // 设置elementDict的部门ID
        elementDict.setDepartId(sysUser.getDeptId().toString());
        return oElementDictMapper.insertElementDict(elementDict);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public OElementDict selectElementDictById(String id) {
        return oElementDictMapper.selectElementDictById(id);
    }

    /**
     * 根据id修改
     * @param elementDict
     * @return
     */
    @Override
    public int updateElementDict(OElementDict elementDict) {
        SysUser sysUser = ShiroUtils.getSysUser();
        elementDict.setUpdateName(sysUser.getUserName());
        elementDict.setUpdateBy(sysUser.getLoginName());
        elementDict.setUpdateDate(DateUtils.getNowDate());
        return oElementDictMapper.updateElementDict(elementDict);
    }

    @Override
    public int deleteElementDictById(String id) {
        return oElementDictMapper.deleteElementDictById(id);
    }
    @Override
    public int deleteElementDictByIds(String ids) {
        return oElementDictMapper.deleteElementDictByIds(Convert.toStrArray(ids));
    }
}
